<?php

include 'db_connection.php';
include 'navigation.php';

$test_series = $_POST['test_series'];
$test_type = $_POST['test_type'];

echo "Test Series: ".$test_series."<br/>";
echo "Test Type: ".$test_type."<br/>";

if ($_FILES["battLog"]["error"] > 0){
  echo "Error: " . $_FILES["battLog"]["error"] . "<br />"; 
  exit(1);
}else{
  echo "Upload: " . $_FILES["battLog"]["name"] . "<br />";
  echo "Type: " . $_FILES["battLog"]["type"] . "<br />";
  echo "Size: " . ($_FILES["battLog"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["battLog"]["tmp_name"];
}

if ($_FILES["locksLog"]["error"] > 0){
  echo "Error: " . $_FILES["locksLog"]["error"] . "<br />"; 
  exit(1);
}else{
  echo "Upload: " . $_FILES["locksLog"]["name"] . "<br />";
  echo "Type: " . $_FILES["locksLog"]["type"] . "<br />";
  echo "Size: " . ($_FILES["locksLog"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["locksLog"]["tmp_name"];
}


$battLogFileName = $_FILES["battLog"]["tmp_name"];
$locksLogFileName = $_FILES["locksLog"]["tmp_name"];


echo "<br/><br/>";

$query0 = "insert into TEST values($test_series, '$test_type')";
$QueryResult0 = @mysqli_query($DBConnect, $query0) or die("<p>Unable to execute query0.</p>");	




	if (($fd = fopen($battLogFileName, "r")) !== FALSE) {

		//initial time
		$time = 0;

    		//get a line and store each cell value in $row array
		while (($row = fgetcsv($fd, 1000, ",")) !== FALSE) {
    
			//remove the string pattern 'charge' from that charge cell in batt log
			$charge = $row[1];

			$charge = str_replace('charge','',$charge);       
 			//convert charge to mA
			$charge = $charge/1000;		
    
			$query = "insert into BATTERY_LOG values($test_series, '$test_type', $time, $charge)";
			$QueryResult = @mysqli_query($DBConnect, $query) or die("<p>Unable to execute query.</p>");			
   
			//echo row to screen
    			echo $time.",".$charge."<br/>";

			//set time for next row
			$time = $time + 5;

		}
		fclose($fd);

	}	



	if (($fd = fopen($locksLogFileName, "r")) !== FALSE) {

		//initial time, will change to 0 after first line
		$time = -5;

    		//get a line and store each cell value in $row array
		while (($row = fgetcsv($fd, 1000, ",")) !== FALSE) {

			//remove the string pattern name: from that name cell in locks log
			$name = $row[1];
			$name = str_replace('name:','',$name);
			
			if($name == 'name'){
				$time = $time + 5;	
			}

			//remove the string pattern 'uptime:' from that uptime cell in locks log
			$uptime = $row[2];
			$uptime = str_replace('uptime:','',$uptime);       
	    
			$query1 = "insert into LOCKS_LOG values('', $test_series, '$test_type', $time, '$name', $uptime)";
			$QueryResult1 = @mysqli_query($DBConnect, $query1) or die("<p>Unable to execute query1.</p>");			
   
			//echo row to screen
    			echo $time.",".$name.",".$uptime."<br/>";

		}
		fclose($fd);

	}	


?>
